package com.rdc.qingkou.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.githang.statusbar.StatusBarCompat;
import com.rdc.qingkou.Application.MyApplication;
import com.rdc.qingkou.R;
import com.rdc.qingkou.config.URL;
import com.rdc.qingkou.contract.UserInfoContract;
import com.rdc.qingkou.entity.ModifyUserInfoResult;
import com.rdc.qingkou.presenter.UserInfoPresenter;

import de.hdodenhof.circleimageview.CircleImageView;


public class PersonalSettingActivity extends AppCompatActivity implements View.OnClickListener,UserInfoContract.IView{

    private RelativeLayout settings_personalInfo_relativeLayout;
    private RelativeLayout settings_bindAccount_relativeLayout;
    private RelativeLayout settings_changePassword_relativeLayout;
    private RelativeLayout settings_feedbackSuggestions_relativeLayout;
    private RelativeLayout settings_clearCache_relativeLayout;
    private Button btn_exit;
    private Button btn_back;
//    private Button btn_message;

    private CircleImageView settings_portrait_circleImageView;
    private TextView settings_name_textView;
    private TextView settings_nametag_textview;
    private TextView settings_clearCache_textView;

    private UserInfoPresenter mUserInfoPresenter;

    @Override
    protected void onResume() {
        super.onResume();
        mUserInfoPresenter.getUserInfo(MyApplication.getUser().getUserId());
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
        setContentView(R.layout.activity_personal_settings);
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.windowBackground), true);
        mUserInfoPresenter = new UserInfoPresenter(this);
        initView();
    }

    private void initView(){
        settings_personalInfo_relativeLayout=(RelativeLayout)findViewById(R.id.settings_personalInfo_relativeLayout);
        settings_bindAccount_relativeLayout=(RelativeLayout)findViewById(R.id.settings_bindAccount_relativeLayout);
        settings_changePassword_relativeLayout=(RelativeLayout)findViewById(R.id.settings_changePassword_relativeLayout);
        settings_feedbackSuggestions_relativeLayout=(RelativeLayout)findViewById(R.id.settings_feedbackSuggestions_relativeLayout);
        settings_clearCache_relativeLayout=(RelativeLayout)findViewById(R.id.settings_clearCache_relativeLayout);
        btn_exit=(Button)findViewById(R.id.btn_exit);
        btn_back=(Button)findViewById(R.id.btn_back);
//        btn_message=(Button)findViewById(R.id.btn_message);

        settings_portrait_circleImageView = (CircleImageView)findViewById(R.id.settings_portrait_circleImageView);
        settings_name_textView = (TextView)findViewById(R.id.settings_name_textView);
        settings_nametag_textview = (TextView)findViewById(R.id.settings_nametag_textview);
        settings_clearCache_textView = (TextView)findViewById(R.id.settings_clearCache_textView);

        settings_personalInfo_relativeLayout.setOnClickListener(this);
        settings_bindAccount_relativeLayout.setOnClickListener(this);
        settings_changePassword_relativeLayout.setOnClickListener(this);
        settings_feedbackSuggestions_relativeLayout.setOnClickListener(this);
        settings_clearCache_relativeLayout.setOnClickListener(this);
        btn_exit.setOnClickListener(this);
        btn_back.setOnClickListener(this);
//        btn_message.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.settings_personalInfo_relativeLayout:
                startActivity(new Intent(PersonalSettingActivity.this,PersonalInfoActivity.class));
                break;
            case R.id.settings_bindAccount_relativeLayout:
                startActivity(new Intent(PersonalSettingActivity.this,BindAccountActivity.class));
                break;
            case R.id.settings_changePassword_relativeLayout:
                startActivity(new Intent(PersonalSettingActivity.this,ChangePasswordActivity.class));
                break;
            case R.id.settings_feedbackSuggestions_relativeLayout:
                startActivity(new Intent(PersonalSettingActivity.this,FeedbackSuggestionsActivity.class));
                break;
            case R.id.settings_clearCache_relativeLayout:
                startActivity(new Intent(PersonalSettingActivity.this,ClearCacheDialogActivity.class));
                break;
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_exit:
                startActivity(new Intent(PersonalSettingActivity.this,ExitApplicationDialogActivity.class));
                break;
//            case R.id.btn_message:
////                startActivity(new Intent(PersonalSettingActivity.this,MessageActivity.class));
//                break;
        }
    }

    @Override
    public void showData(ModifyUserInfoResult modifyUserInfoResult) {
//        Glide.with(this).load(URL.HEAD_YUN+modifyUserInfoResult.getUser().getImageUrl())
//                .placeholder(R.drawable.imageview_default).dontAnimate().into(settings_portrait_circleImageView);

//        if (modifyUserInfoResult.getUser().getImageUrl() != null) {
//            Glide.with(this).load(URL.HEAD_YUN2+modifyUserInfoResult.getUser().getImageUrl()).into(settings_portrait_circleImageView);
//        } else {
//            settings_portrait_circleImageView.setImageDrawable(getResources().getDrawable(R.drawable.user));
//        }

        if (MyApplication.getUser().getImageUrl() != null) {
            Glide.with(this).load(URL.HEAD_YUN2+modifyUserInfoResult.getUser().getImageUrl()).into(settings_portrait_circleImageView);
        }else {
            settings_portrait_circleImageView.setImageDrawable(getResources().getDrawable(R.drawable.user));
        }

        if (modifyUserInfoResult.getUser().getNickName() != null){
            settings_name_textView.setText(modifyUserInfoResult.getUser().getNickName().replace("\"",""));
        }else {
            settings_name_textView.setText("Candy");
        }

        if (modifyUserInfoResult.getUser().getSignature() != null){
            settings_nametag_textview.setText(modifyUserInfoResult.getUser().getSignature().replace("\"",""));
        }else {
            settings_nametag_textview.setText("每一首情歌忽然充满意义，我就在此刻突然见到你");
        }
    }

    @Override
    public void showModifyResult(ModifyUserInfoResult modifyUserInfoResult) {
        Log.d("PersonalSettingActivity","RESULT : " + modifyUserInfoResult.getResult());

    }

    @Override
    public void showInfo(String info) {
        Log.d("PersonalSettingActivity","ERROR : " + info);
    }
}
